package com.xd.common.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Redisson初始化配置类
 */
@Configuration
@Profile({"dev", "prd", "tengxundev", "zx"})
public class RedissonConfig {

    private static final String REDISSON_PREFIX = "redis://";

    @Value("${spring.redis.host}")
    private String redisAddr;

    @Value("${spring.redis.database}")
    private Integer database;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient redissonClient() {
        //创建配置类
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer().
                setAddress(String.join("", REDISSON_PREFIX, redisAddr, ":6379"));
        //配置Redis访问密码
        serverConfig.setPassword(password);
        //设置最小连接数
        serverConfig.setConnectionMinimumIdleSize(1);
        //配置链接数据库0-15，默认：0
        serverConfig.setDatabase(database);
        return Redisson.create(config);
    }

}
